 #include <QObject>
#include "serialportconnector.hpp"
#include "stringlogger.hpp"

SerialPortConnector::SerialPortConnector()
    :serial("COM1", QextSerialPort::Polling)
{

}
bool SerialPortConnector::connectPort(size_t portNumber)
{

    serial.setBaudRate(BAUD9600);
    serial.setFlowControl(FLOW_OFF);
    serial.setParity(PAR_NONE);
    serial.setDataBits(DATA_8);
    serial.setStopBits(STOP_2);
    //set timeouts to 500 ms
    serial.setTimeout(500);
    return serial.open(QIODevice::ReadWrite);

}
Data SerialPortConnector::receive(size_t timeoutInMs)
{

    DataType resp[] = {0x82, 0x13, 0x58, 0xE1, 0xCE};
    Data tempresp = Data(resp, resp+5);
    Data retval  = lastmsg_;
    retval.insert(retval.end(), tempresp.begin(), tempresp.end());
    LOG << "SerialPortConnector::receive <- " << retval ;
    return retval;
}
bool SerialPortConnector::send(const Data& data)
{
    LOG << "SerialPortConnector::send    -> " << data ;
    lastmsg_ = data;
    return true;
}
void SerialPortConnector::onData()
{
    QByteArray data = serial.readAll();
    storeData(data);
}
void SerialPortConnector::storeData(QByteArray data)
{
    for(QByteArray::iterator i = data.begin(); i!= data.end();++i)
    {

    }

}
/*

The package contains a qextserialport.pri file that allows you to integrate the component into programs that use qmake for the build step.
Download the source code.
Put the source code in any directory you like. For example, 3rdparty:
|-- project.pro
|-- ....
|-- 3rdparty\
|     |-- qextserialport\
|     |
Add following line to your qmake project file:
include(3rdparty/qextserialport/src/qextserialport.pri)
Then, using QextSerialPort in your code

    #include "qextserialport.h"
    ...
    MyClass::MyClass()
    {
        port = new QextSerialPort("COM1");
        connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
        port->open();
    }

    void MyClass::onDataAvailable()
    {
        QByteArray data = port->readAll();
        processNewData(data);
    }
 */
